// components/picker/mypicker.js

import { provinces,citys,areas } from '../../../utils/city'

const hunan = provinces.filter(v=>v.name=="湖南省")[0]
const hunanCity = citys[hunan.id]
const changsha = areas[hunanCity.filter(v=>v.name=="长沙市")[0].id]

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    address:{ 
      type:Array,
      value:()=>[],
      
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    hunan,// 湖南省

    cityCol:hunanCity,// 城市 默认：长沙
    areaCol:changsha,// 区 默认：天心区

    multiArray:[],
    multiIndex:[0,0],
    address:"",

    showPicker:0,// 是否显示picker
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindChange(e){
      console.log("bindChange",e.detail.value);
      let [cityIndex,areaIndex] = e.detail.value 
      if(cityIndex!=this.data.multiIndex[0]){
        console.log("选择其他城市");
        areaIndex = 0;
      }
      this.setData({
        areaCol: areas[hunanCity[cityIndex].id],
        multiIndex: [cityIndex,areaIndex]
      },()=>{
        this.triggerEvent("choseDone",[
          this.data.cityCol[cityIndex].name,
          this.data.areaCol[areaIndex].name]
        )
      }) 
    },
    // 取消
    cancel(){
      this.setData({
        showPicker:0 
      })
    },
    setPicker(){
      
    },
    // 确定
    sure(){
      // setTimeout(()=>{
        this.setData({ showPicker:0 })
        let { cityCol,areaCol,multiIndex:[cityIndex,areaIndex] } = this.data
        this.triggerEvent("choseDone",[
          this.data.cityCol[this.data.multiIndex[0]].name,
          this.data.areaCol[this.data.multiIndex[1]].name]
          )
      // },500)
      console.log("确定")
    },
    showPicker(index){
      this.setData({
        showPicker:1 
      })
    },
  },
  
})
